/**
 * 
 */
package FileParse;

/**
 * @author Anarki
 *
 */
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
 
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XMLParser {
	
	
	//uzima sve korisnike i informacije
	public void getAllUsers(String fileName) {
        try {
        	
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            
            File file = new File(fileName);
            if (file.exists()) {
                Document doc = db.parse(file);
                Element docEle = doc.getDocumentElement();
 
                // Print root element of the document
//                System.out.println("Root element of the document: "
//                        + docEle.getNodeName());
 
                NodeList listaKorisnika = docEle.getElementsByTagName("korisnik");
 
                // Print total student elements in document
                System.out.println("Total students: " + listaKorisnika.getLength());
 
                if (listaKorisnika != null && listaKorisnika.getLength() > 0) {
                    for (int i = 0; i < listaKorisnika.getLength(); i++) {
 
                        Node node = listaKorisnika.item(i);
 
                        if (node.getNodeType() == Node.ELEMENT_NODE) {
 
                            Element e = (Element) node;
                            NodeList nodeList = e.getElementsByTagName("ime");
                            System.out.println("Name: " + nodeList.item(0).getChildNodes().item(0).getNodeValue());
 
                            nodeList = e.getElementsByTagName("prezime");
                            System.out.println("Grade: "+ nodeList.item(0).getChildNodes().item(0).getNodeValue());
 
                            nodeList = e.getElementsByTagName("datum");
                            System.out.println("Age: "+ nodeList.item(0).getChildNodes().item(0).getNodeValue());
                            
                            nodeList = e.getElementsByTagName("jmbg");
                            System.out.println("Age: "+ nodeList.item(0).getChildNodes().item(0).getNodeValue());
                            
                            nodeList = e.getElementsByTagName("godinaKluba");
                            System.out.println("Age: "+ nodeList.item(0).getChildNodes().item(0).getNodeValue());
                            
                            nodeList = e.getElementsByTagName("adresa");
                            System.out.println("Age: "+ nodeList.item(0).getChildNodes().item(0).getNodeValue());
                            
                            nodeList = e.getElementsByTagName("pojas");
                            System.out.println("Age: "+ nodeList.item(0).getChildNodes().item(0).getNodeValue());
                        }
                    }
                } else {
                    System.exit(1);
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
   
}
